package win.smartown.app.gank.day.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import win.smartown.app.base.mvp.BasePresenter;
import win.smartown.app.gank.entity.Data;
import win.smartown.app.gank.entity.GankResponse;
import win.smartown.app.gank.entity.MultipleData;
import win.smartown.app.request.RequestListener;
import win.smartown.app.request.RequestManager;

/**
 * Created by smartown on 2017/5/5 15:17.
 * Desc:
 */
public class DayDataPresenter extends BasePresenter<Contract.View> implements Contract.Presenter {

    private List<MultipleData> dataList;
    private Date date;

    public DayDataPresenter(Contract.View view) {
        super(view);
        dataList = new ArrayList<>();
    }

    @Override
    public void getDayData(int year, int month, final int day) {
        dataList.clear();
        getView().onDataChanged();
        getView().getSuperActivity().request(RequestManager.GankManager.getDayData(year, month, day), new RequestListener<GankResponse<HashMap<String, List<Data>>>>() {
            @Override
            public void onStart() {
                getView().startLoading();
            }

            @Override
            public void onFail(Throwable e) {
                getView().finishLoading();
            }

            @Override
            public void onSuccess(GankResponse<HashMap<String, List<Data>>> result) {
                getView().finishLoading();
                if (!result.isError()) {
                    final List<String> types = result.getCategory();
                    for (String type : types) {
                        dataList.add(new MultipleData(1, type));
                        final List<Data> list = result.getResults().get(type);
                        for (Data data : list) {
                            dataList.add(new MultipleData(2, data));
                        }
                    }
                    getView().onDataChanged();
                }
            }
        });
    }

    public List<MultipleData> getDataList() {
        return dataList;
    }
}
